package com.molichuxing.services.infrastructure.service.impl;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.exception.BizException;
import com.molichuxing.services.infrastructure.bean.MaintenanceConfigBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.MaintenanceConfigMapper;
import com.molichuxing.services.infrastructure.dto.request.create.MaintenanceConfigCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.MaintenanceConfigModifyDto;
import com.molichuxing.services.infrastructure.dto.response.MaintenanceConfigDto;
import com.molichuxing.services.infrastructure.service.MaintenanceConfigService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 保养配置表 服务实现类
 * </p>
 *
 * @author xuelin yi
 * @since 2019-09-23
 */
@Service("maintenanceConfigService")
public class MaintenanceConfigServiceImpl implements MaintenanceConfigService {

    @Resource
    private MaintenanceConfigMapper maintenanceConfigMapper;

    @Override
    public MaintenanceConfigDto getMaintenanceConfigByVehicleId(Integer vehicleId) {
        MaintenanceConfigBean maintenanceConfigBean = maintenanceConfigMapper.getMaintenanceConfigByVehicleId(vehicleId);
        return Convert.toMaintenanceConfigDto(maintenanceConfigBean);
    }

    /**
     * 分页
     *
     * @param map
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Paged<MaintenanceConfigDto> getMaintenanceConfigPage(Map<String, Object> map, Integer pageNum, Integer pageSize) {
        long pageCount = maintenanceConfigMapper.getPageCount(map);
        List<MaintenanceConfigBean> beanList = maintenanceConfigMapper.getPage(map, pageNum, pageSize);
        List<MaintenanceConfigDto> dtoList = Convert.toMaintenanceConfigDtoList(beanList);
        return new Paged<MaintenanceConfigDto>(dtoList, pageCount, pageSize);
    }

    /**
     * 查询
     *
     * @param id
     * @return
     */
    @Override
    public MaintenanceConfigDto getMaintenanceConfigById(Integer id) {
        MaintenanceConfigBean maintenanceConfigBean = maintenanceConfigMapper.getById(id);
        return Convert.toMaintenanceConfigDto(maintenanceConfigBean);
    }

    /**
     * 添加
     *
     * @param dto
     * @return
     */
    @Override
    public Integer createMaintenanceConfig(MaintenanceConfigCreateDto dto) {
        Integer vehicleId = dto.getVehicleId();
        MaintenanceConfigBean oldMaintenanceConfigBean = maintenanceConfigMapper.getMaintenanceConfigByVehicleId(vehicleId);
        if (oldMaintenanceConfigBean != null) {
            throw new BizException("车型保养预警已存在");
        }

        MaintenanceConfigBean maintenanceConfigBean = Convert.toMaintenanceConfigBean(dto);
        maintenanceConfigMapper.create(maintenanceConfigBean);
        return maintenanceConfigBean.getId();
    }

    /**
     * 修改
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean modifyMaintenanceConfig(MaintenanceConfigModifyDto dto) {
        Integer id = dto.getId();
        MaintenanceConfigBean oldMaintenanceConfigBean = maintenanceConfigMapper.getById(id);
        if (null == oldMaintenanceConfigBean) {
            throw new BizException("车型保养预警不存在");
        }

        MaintenanceConfigBean maintenanceConfigBean = Convert.toMaintenanceConfigBean(dto);
        maintenanceConfigMapper.modifyById(maintenanceConfigBean);
        return true;
    }

    @Override
    public List<Integer> getVehicleIdList() {
        return maintenanceConfigMapper.getVehicleIdList();
    }

}
